﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DMS.DAL;

namespace DMS.Logic
{
    public class BL
    {
        #region global
        private static BL _instance = null;
        public static BL getInstance()
        {
            if (_instance == null)
                _instance = new BL();
            return _instance;
        }

        private BL()
        {
        }
        #endregion
        #region library
        public void saveDocumentLibrary(DocumentLibrary dl, int StorageProviderId,int userId)
        {
            dl.User = getUserById(userId);
            dl.StorageProvider=getStorageProviderById(StorageProviderId);
            DALclass.getInstance().saveDocumentLibrary(dl);
        }

        public DocumentLibrary getDocumentLibraryById(int documentLibraryId)
        {
            return DALclass.getInstance().getDocumentLibraryById(documentLibraryId);
        }
        
        public List<DocumentLibrary> getAllDocumentLibrary()
        {
            return DALclass.getInstance().getAllDocumentLibrary();
        }
        public void deleteDocumentLibrary(int documentLibraryId)
        {
            DocumentLibrary dl = DALclass.getInstance().getDocumentLibraryById(documentLibraryId);
            if (dl != null)
                DALclass.getInstance().deleteObject(dl);

        }
        #endregion
        #region Folders
        public List<Folder> getFoldersByLibrary(int libraryId, int current)
        {
            return DALclass.getInstance().getFoldersByLibrary(libraryId, current);
        }
        public List<Folder> getFoldersByLibraryAndFolder(int libraryId, int folderId, int current)
        {
            if (folderId == 0)
                return DALclass.getInstance().getFoldersByLibrary(libraryId, current);
            else
                return DALclass.getInstance().getFoldersByLibraryAndFolder(libraryId, folderId, current);
        }
        public Folder getFolderById(int folderId)
        {
            return DALclass.getInstance().getFolderById(folderId);
        }
        public int saveFolder(Folder fldr, int documentLibraryId, int parentFolderId, int userId)
        {
            fldr.User = getUserById(userId);
            fldr.DocumentLibrary = getDocumentLibraryById(documentLibraryId);
            fldr.Folder2 = getFolderById(parentFolderId);
            return DALclass.getInstance().saveFolder(fldr);
        }
        public int updateFolder(Folder fldr, int documentLibraryId, int parentFolderId, int userId)
        {
            Folder nf = getFolderById(parentFolderId);
            if (nf != fldr.Folder2 && fldr.Folder1.Count > 0)
            {
                return -1;
            }
            fldr.Folder2 = nf;
            fldr.DocumentLibrary = getDocumentLibraryById(documentLibraryId);
            fldr.User = getUserById(userId);
            return DALclass.getInstance().saveFolder(fldr);
        }
        public void deleteFolder(int fldrId)
        {
            Folder fldr = getFolderById(fldrId);
            if (fldr.Folder1.Count > 0 || fldr.Document.Count>0)
                return;
            DALclass.getInstance().deleteObject(fldr);
        }
        #endregion
        #region user
        public User getUserById(int id)
        {
            return DALclass.getInstance().getUserById(id);
        }
        public List<User> getAllUser()
        {
            return DALclass.getInstance().getAllUser();
        }
        public bool canUserAccessTHeLibrary(int userId,int LibId)
        {
            DocumentLibrary dl = getDocumentLibraryById(LibId);
            if (dl.User.ID == userId)
                return true;
            else
            {
                return getUserPermission(userId, LibId)!=null;
            }
            return false;
        }
        #endregion
        #region userpermission
        public List<UserPermission> getUserPermissionsForDocumentLibrary(int documentLibraryId)
        {
            return DALclass.getInstance().getUserPermissionForDocumentLibrary(documentLibraryId);
        }
        public int saveUserPermission(UserPermission up)
        {
            up.DocumentLibrary = getDocumentLibraryById(up.DocumentLibraryID);
            up.User = getUserById(up.UserID);
            return DALclass.getInstance().saveUserPermission(up);
        }
        public void deleteUserPermission(int userId, int documentLibId)
        {
            UserPermission up = DALclass.getInstance().getUserPermission(userId, documentLibId);
            DALclass.getInstance().deleteObject(up);
        }
        public UserPermission getUserPermission(int userId, int documentLibId)
        {
            return DALclass.getInstance().getUserPermission(userId, documentLibId);
        }
        #endregion
        #region StorageProvider
        public StorageProvider getStorageProviderById(int id)
        {
            return DALclass.getInstance().getStorageProviderById(id);
        }
        public List<StorageProvider> getAllStorageProvider()
        {
            return DALclass.getInstance().getAllStorageProvider();
        }
        #endregion

		public byte[] GetDocumentData(int documentId)
		{
			return DALclass.getInstance().GetDocumentData(documentId);
		}

		public DMS.DAL.Document GetDocumentById(int documentId)
		{
			return DALclass.getInstance().GetDocumentById(documentId);
		}
	}
}
